import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/front/Home.vue')
  },
  {
    path: '/videos',
    name: 'Videos',
    component: () => import('@/views/front/Videos.vue')
  },
  {
    path: '/video/:id',
    name: 'VideoDetail',
    component: () => import('@/views/front/VideoDetail.vue')
  },
  {
    path: '/categories',
    name: 'Categories',
    component: () => import('@/views/front/Categories.vue')
  },
  {
    path: '/category/:id',
    name: 'CategoryDetail',
    component: () => import('@/views/front/CategoryDetail.vue')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/front/Search.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/front/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/auth',
    name: 'Auth',
    component: () => import('@/views/AuthView.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/views/admin/AdminLayout.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/Dashboard.vue')
      },
      {
        path: 'videos',
        name: 'AdminVideos',
        component: () => import('@/views/admin/Videos.vue')
      },
      {
        path: 'categories',
        name: 'AdminCategories',
        component: () => import('@/views/admin/Categories.vue')
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('@/views/admin/Users.vue')
      },
      {
        path: 'settings',
        name: 'AdminSettings',
        component: () => import('@/views/admin/Settings.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// Navigation guard
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  const isAdmin = localStorage.getItem('isAdmin') === 'true'

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/auth')
  } else if (to.meta.requiresAdmin && !isAdmin) {
    next('/')
  } else {
    next()
  }
})

export default router 